<html>
<head><meta charset="utf-8"><title>Transparent newtype with custom alignment · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transparent.20newtype.20with.20custom.20alignment.html">Transparent newtype with custom alignment</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="202923329"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transparent%20newtype%20with%20custom%20alignment/near/202923329" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transparent.20newtype.20with.20custom.20alignment.html#202923329">(Jul 05 2020 at 14:13)</a>:</h4>
<p>I have a use case for transmuting a reference to a struct containing only a <code>[u8; 4]</code>, but the struct should have an alignment of 4. Is there a way to accomplish that?</p>
<p>I tried to use <code>#[repr(transparent, align(4))]</code>, but the compiler refuses to accept that, saying they conflict. My understanding is that <code>#[repr(align(4))]</code> doesn't guarantee anything about the inner layout, so how would I accomplish this?</p>



<a name="202924817"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transparent%20newtype%20with%20custom%20alignment/near/202924817" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transparent.20newtype.20with.20custom.20alignment.html#202924817">(Jul 05 2020 at 14:56)</a>:</h4>
<p>Well if the struct contains only a single field you could use <code>#[repr(C, align(4))]</code></p>



<a name="202925119"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transparent%20newtype%20with%20custom%20alignment/near/202925119" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transparent.20newtype.20with.20custom.20alignment.html#202925119">(Jul 05 2020 at 15:04)</a>:</h4>
<p><span class="user-mention" data-user-id="211727">@Jonas Schievink</span> you don't need <code>transparent</code> for <em>transmuting</em></p>



<a name="202925124"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transparent%20newtype%20with%20custom%20alignment/near/202925124" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transparent.20newtype.20with.20custom.20alignment.html#202925124">(Jul 05 2020 at 15:04)</a>:</h4>
<p>But I need some defined layout that matches how <code>[u8; 4]</code> looks in memory, right?</p>



<a name="202925129"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transparent%20newtype%20with%20custom%20alignment/near/202925129" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transparent.20newtype.20with.20custom.20alignment.html#202925129">(Jul 05 2020 at 15:04)</a>:</h4>
<p><code>transparent</code> allows <code>fn(Foo)</code> and <code>fn([u8; 4])</code> to be interchangeable (although maybe only for <code>extern "C"</code>)</p>



<a name="202925131"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transparent%20newtype%20with%20custom%20alignment/near/202925131" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transparent.20newtype.20with.20custom.20alignment.html#202925131">(Jul 05 2020 at 15:04)</a>:</h4>
<p>that's not what <code>transparent</code> does, although it kind of has to <em>also</em> do that</p>



<a name="202925136"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transparent%20newtype%20with%20custom%20alignment/near/202925136" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transparent.20newtype.20with.20custom.20alignment.html#202925136">(Jul 05 2020 at 15:05)</a>:</h4>
<p>defined <em>memory</em> layout is just <code>repr(C)</code> like <span class="user-mention" data-user-id="249222">@Elichai Turkel</span> suggested</p>



<a name="202925140"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transparent%20newtype%20with%20custom%20alignment/near/202925140" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transparent.20newtype.20with.20custom.20alignment.html#202925140">(Jul 05 2020 at 15:05)</a>:</h4>
<p>and AFAIK <code>#[repr(C)]</code> can look different than just the inner field on some platforms, right?</p>



<a name="202925186"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transparent%20newtype%20with%20custom%20alignment/near/202925186" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transparent.20newtype.20with.20custom.20alignment.html#202925186">(Jul 05 2020 at 15:06)</a>:</h4>
<p>only by <em>trailing</em> padding AFAIK, which would result in a different size, which would cause a compilation error because <code>transmute</code>, I guess?</p>



<a name="202925188"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transparent%20newtype%20with%20custom%20alignment/near/202925188" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transparent.20newtype.20with.20custom.20alignment.html#202925188">(Jul 05 2020 at 15:06)</a>:</h4>
<p>but I can't remember if that's possible with byte arrays at all</p>



<a name="202925452"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transparent%20newtype%20with%20custom%20alignment/near/202925452" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transparent.20newtype.20with.20custom.20alignment.html#202925452">(Jul 05 2020 at 15:13)</a>:</h4>
<p>I see, that should work then. Thanks.</p>



<a name="202926266"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transparent%20newtype%20with%20custom%20alignment/near/202926266" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transparent.20newtype.20with.20custom.20alignment.html#202926266">(Jul 05 2020 at 15:37)</a>:</h4>
<p><span class="user-mention silent" data-user-id="211727">Jonas Schievink</span> <a href="#narrow/stream/122651-general/topic/Transparent.20newtype.20with.20custom.20alignment/near/202925140">said</a>:</p>
<blockquote>
<p>and AFAIK <code>#[repr(C)]</code> can look different than just the inner field on some platforms, right?</p>
</blockquote>
<p>a C struct with a single field always match the field's memory layout (unless you explicitly override the alignment obviously)</p>



<a name="202930491"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transparent%20newtype%20with%20custom%20alignment/near/202930491" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transparent.20newtype.20with.20custom.20alignment.html#202930491">(Jul 05 2020 at 17:30)</a>:</h4>
<p><span class="user-mention" data-user-id="249222">@Elichai Turkel</span> That's true, but that doesn't necessarily mean that the structure or its single field will always be passed the same way if they are an argument to a function. Some targets have interesting ABIs for passing structures.</p>



<a name="202930503"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transparent%20newtype%20with%20custom%20alignment/near/202930503" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transparent.20newtype.20with.20custom.20alignment.html#202930503">(Jul 05 2020 at 17:31)</a>:</h4>
<p>For instance, a u64 may be passed in a register, while a struct containing a u64 may be passed as a pointer.</p>



<a name="202931510"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transparent%20newtype%20with%20custom%20alignment/near/202931510" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transparent.20newtype.20with.20custom.20alignment.html#202931510">(Jul 05 2020 at 18:03)</a>:</h4>
<p><span class="user-mention" data-user-id="239881">@Josh Triplett</span> That's really interesting!  what arch does this? where can I read on that? :)</p>



<a name="202932034"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transparent%20newtype%20with%20custom%20alignment/near/202932034" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transparent.20newtype.20with.20custom.20alignment.html#202932034">(Jul 05 2020 at 18:17)</a>:</h4>
<p>I don't know which ABIs do that off the top of my head. I know that some do, and some don't.</p>



<a name="202932906"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transparent%20newtype%20with%20custom%20alignment/near/202932906" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transparent.20newtype.20with.20custom.20alignment.html#202932906">(Jul 05 2020 at 18:40)</a>:</h4>
<p>I think SPARC or s390x or maybe PPC, but I do remember one of the obscurer targets supported by Rust having different ABI in that respect.</p>



<a name="202944219"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transparent%20newtype%20with%20custom%20alignment/near/202944219" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transparent.20newtype.20with.20custom.20alignment.html#202944219">(Jul 06 2020 at 00:13)</a>:</h4>
<p><span class="user-mention" data-user-id="239881">@Josh Triplett</span> yeah, that's why we were talking about memory layout specifically</p>



<a name="202944259"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transparent%20newtype%20with%20custom%20alignment/near/202944259" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transparent.20newtype.20with.20custom.20alignment.html#202944259">(Jul 06 2020 at 00:14)</a>:</h4>
<p>for transmute, memory layout matching is enough, no need to worry about call ABI and <code>#[transparent]</code></p>



<a name="202944348"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transparent%20newtype%20with%20custom%20alignment/near/202944348" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transparent.20newtype.20with.20custom.20alignment.html#202944348">(Jul 06 2020 at 00:17)</a>:</h4>
<p>the x86_64 SysV ABI (the one used everywhere outside of windows) is actually kind of unique in how aggressive it is at looking inside types, and even combining the cases of <code>union</code>s IIRC</p>



<a name="202944412"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transparent%20newtype%20with%20custom%20alignment/near/202944412" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transparent.20newtype.20with.20custom.20alignment.html#202944412">(Jul 06 2020 at 00:19)</a>:</h4>
<p>32-bit arches (x86 and ARM?) should have some examples where a <code>struct</code> is passed more opaquely than primitives, mostly because they predate ABIs getting more aggressive, I think</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>